import enum

import PackageAnalyse.PackageAnalyseConfig as Pg
from PackageAnalyse.PackageAnalyseTool import PackageAnalyseTool as Pt


class PackageAnalyse:
    @staticmethod
    def load_url_asset(projectName: str, version: str, file_type: enum.Enum) -> dict:
        """
        调用加载获取的文件内容
        :param projectName: 项目名
        :param version: 资源版本号
        :param file_type: 获取文件类型
        :return:
        """
        return Pt.get_url_asset_data(Pg.ProjectAssetStore[projectName].value + version + file_type.value)

    @staticmethod
    def load_local_asset(path: str) -> dict:
        """
        调用加载本地文件内容
        :param path: 文件路径
        :return:
        """
        return Pt.get_local_file_asset_data(path)

    @staticmethod
    def get_asset_data(content: dict, get_by_key: enum.Enum) -> set:
        """
        反射调用获取文件内容所有资源路径
        :param content: 文件内容
        :param get_by_key: key值获取所需内容
        :return:
        """
        call_method = 'get_asset_data_in_' + get_by_key.name
        if call_method in Pt.__dict__:
            method = getattr(Pt, call_method)
            if callable(method):
                return method(content, get_by_key.value)

    @staticmethod
    def load_check_asset(path: str, key: enum.Enum):
        """
        调用加载安装包分析输出out_xxx.json文件
        :param path: 文件路径
        :param key: 键名
        :return:
        """
        return Pt.get_check_asset_data(path, key.value)

    @staticmethod
    def asset_path_write_to_csv(header, data, path):
        Pt.save_asset_path_to_csv(header, data, path)


def get_url_asset_path(projectName: str, version: str, file_type: enum.Enum, get_by_key: enum.Enum) -> set:
    """
    获取远程url dependenciesfile 文件资源路径
    :param projectName: 项目名
    :param version: 资源版本号
    :param file_type: 获取文件类型
    :param get_by_key: key值获取所需内容
    :return:
    """
    return PackageAnalyse.get_asset_data(PackageAnalyse.load_url_asset(projectName, version, file_type), get_by_key)


def get_local_asset_path(path: str, get_by_key: enum.Enum):
    """
    获取本地 dependenciesfile 文件资源路径
    :param path: 文件路径
    :param get_by_key: key值获取所需内容
    :return:
    """
    return PackageAnalyse.get_asset_data(PackageAnalyse.load_local_asset(path), get_by_key)


def get_check_asset(path: str, key: enum.Enum):
    """
    加载 本地out_xxx.json文件资源， 返回字典键名为资源路径地址
    :param path: 文件路径
    :param key: 键名
    :return:
    """
    return PackageAnalyse.load_check_asset(path, key)


def get_newly_added_asset_list(projectName, version_A, version_B, file_type, get_by_key_A, get_by_key_B):
    """
    获取新增资源列表
    :param projectName: 项目名称
    :param version_A: Apk版本号 A
    :param version_B: Apk版本号 B
    :param file_type: 读取文件类型
    :param get_by_key_A: 资源提取key值 A
    :param get_by_key_B: 资源提取key值 B
    :return:
    """
    data_a = PackageAnalyse.get_asset_data(PackageAnalyse.load_url_asset(projectName, version_A, file_type),
                                           get_by_key_A)
    data_b = PackageAnalyse.get_asset_data(PackageAnalyse.load_url_asset(projectName, version_B, file_type),
                                           get_by_key_B)
    return data_b.difference(data_a)


def get_reduce_asset_list(projectName, version_A, version_B, file_type, get_by_key_A, get_by_key_B):
    """
    获取移除资源列表
    :param projectName: 项目名称
    :param version_A: Apk版本号 A
    :param version_B: Apk版本号 B
    :param file_type: 读取文件类型
    :param get_by_key_A: 资源提取key值 A
    :param get_by_key_B: 资源提取key值 B
    :return:
    """
    data_a = PackageAnalyse.get_asset_data(PackageAnalyse.load_url_asset(projectName, version_A, file_type),
                                           get_by_key_A)
    data_b = PackageAnalyse.get_asset_data(PackageAnalyse.load_url_asset(projectName, version_B, file_type),
                                           get_by_key_B)
    return data_a.difference(data_b)


def get_asset_path_by_diff_file(header, data, path):
    PackageAnalyse.asset_path_write_to_csv(header, data, path)

